package com.bbcloud.service.impl;

import com.bbcloud.common.Const;
import com.bbcloud.common.ServerResponse;
import com.bbcloud.common.TokenCache;
import com.bbcloud.dao.HelpMapper;
import com.bbcloud.dao.UserMapper;
import com.bbcloud.pojo.Help;
import com.bbcloud.pojo.Link;
import com.bbcloud.pojo.User;
import com.bbcloud.service.IFileService;
import com.bbcloud.service.IHelpService;
import com.bbcloud.util.IDUtil;
import com.bbcloud.util.PropertiesUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Created by liuzhu on 2018/12/19.
 */
@Service("iHelpService")
public class HelpServiceImpl implements IHelpService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HelpMapper helpMapper;
    @Autowired
    private IFileService iFileService;

    public ServerResponse suggestion(String token, String userId, MultipartFile file, String path, String description){
        User user = userMapper.selectUser(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getEmail());
        if (!StringUtils.equals(tokenValid,token)){
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        String targetFileName = iFileService.upload(file,path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        Map fileMap = Maps.newHashMap();
        fileMap.put("uri",targetFileName);
        fileMap.put("url",url);
        Help help = new Help();
        help.setReserveInt1(Const.HelpType.HELPTYPE_SUGGETION);
        help.setHelpImages(url);
        help.setHelpDes(description);
        help.setUserId(userId);
        help.setHelpId(IDUtil.HelpId());
        int rowCount = helpMapper.insert(help);
        if (rowCount>0){
            return ServerResponse.createBySuccess();
        }
        return  ServerResponse.createByError();
    }

    public ServerResponse repair(String token,String userId,MultipartFile file, String path, String description,String longitude,String latitude,String addressDetail,String deviceCheckInfo){
        User user = userMapper.selectUser(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getEmail());
        if (!StringUtils.equals(tokenValid,token)){
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        String targetFileName = iFileService.upload(file,path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        Map fileMap = Maps.newHashMap();
        fileMap.put("uri",targetFileName);
        fileMap.put("url",url);
        Help help = new Help();
        help.setReserveInt1(Const.HelpType.HELPTYPE_REPAIR);
        help.setHelpImages(url);
        help.setHelpDes(description);
        help.setUserId(userId);
        help.setHelpId(IDUtil.HelpId());
        help.setLongitude(longitude);
        help.setLatitude(latitude);
        help.setAddressDetail(addressDetail);
        help.setDeviceCheckInfo(deviceCheckInfo);
        int rowCount = helpMapper.insert(help);
        if (rowCount>0){
            return ServerResponse.createBySuccess();
        }
        return  ServerResponse.createByError();
    }

    public ServerResponse<PageInfo> list(String token, String userId, int pageNum, int pageSize, String orderBy){
        User user = userMapper.selectUser(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getEmail());
        if (!StringUtils.equals(tokenValid,token)){
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Help> helpList = helpMapper.selectHelpsWithUserId(userId);
        PageInfo pageResult = new PageInfo(helpList);
        pageResult.setList(helpList);
        return ServerResponse.createBySuccess(pageResult);
    }
}
